//
//  JLcYhdJLCFLoginViewControllerPAPHbZ.m
//  Jlcf_ios
//
//  Created by Ji on 2018/9/26.
//  Copyright © 2018 ski. All rights reserved.
//

#import "JLcYhdJLCFLoginViewControllerPAPHbZ.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "PzwoeNJLCFForgetPassViewControllerVoiWYJ.h"
#import "dgXPYNJLCFInputViewjVJFSJ.h"

@interface JLcYhdJLCFLoginViewControllerPAPHbZ ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *dZrvUEtextFieldRRHEAt;

@property (nonatomic, strong) UIButton *FDTTddnextBtnLAOJQZ;
@property (nonatomic, strong) dgXPYNJLCFInputViewjVJFSJ *CDozpXinputViewIsapkb;

@end

@implementation JLcYhdJLCFLoginViewControllerPAPHbZ

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self addNavigationBar];
//    self.navigationBar.title = @"登录";
    
//    self.view.backgroundColor = QTZ_Gray237;
//    [self configView];
    [self setUpLoginUI];
    
}

- (void)setUpLoginUI {
    self.KWcnnUtitleTipsLabelQLOdiQ.text = @"账号登录";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"您正在登录账号    " attributes:@{NSFontAttributeName:FONT(10), NSForegroundColorAttributeName:QTZ_Gray153}];
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.UabOWQphoneNumberaYOEQN attributes:@{NSFontAttributeName:FONT(10), NSForegroundColorAttributeName:[UIColor blackColor]}];
    [attStr appendAttributedString:str];
    self.NdgzxmdesLabelTTSqri.attributedText = attStr;
    [self.zNugwOsubmitBtnFCoXrL setTitle:@"登录" forState:UIControlStateNormal];
    
    dgXPYNJLCFInputViewjVJFSJ *CDozpXinputViewIsapkb = [[dgXPYNJLCFInputViewjVJFSJ alloc] initWithInputType:InputTypeMessage];
    CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.delegate = self;
    CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.secureTextEntry = YES;
    CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.returnKeyType = UIReturnKeyDone;
    CDozpXinputViewIsapkb.ajptsrleftTipsImageViewgYkbmv.image = [UIImage imageNamed:@"icon_login_pwd"];
    [self.contentView addSubview:CDozpXinputViewIsapkb];
    [CDozpXinputViewIsapkb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(FitRealValue(78));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-FitRealValue(78));
        make.height.mas_equalTo(FitRealValue(80));
        make.top.mas_equalTo(self.NdgzxmdesLabelTTSqri.mas_bottom).offset(FitRealValue(80));
    }];
    _CDozpXinputViewIsapkb = CDozpXinputViewIsapkb;
    [self.zNugwOsubmitBtnFCoXrL addTarget:self action:@selector(handleLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.CDozpXinputViewIsapkb setInputTextPlaceHolder:@"请输入登录密码"];
    
    UIButton *forgetBtn = [UIButton new];
    [forgetBtn addTarget:self action:@selector(handleForget) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    forgetBtn.titleLabel.font = FONT(12);
    [forgetBtn setTitleColor:QTZ_Gray102 forState:0];
    [forgetBtn setTitle:@"找回密码" forState:0];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-FitRealValue(45));
        make.height.mas_equalTo(FitRealValue(60));
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)configView {
    UILabel *titleLabel = [UILabel labelWithText:@"账号登录" textColor:QTZ_Gray51 font:FONT(25)];
    titleLabel.left = 40;
    titleLabel.top = NAVBAR_HEIGHT + 75;
    [self.view addSubview:titleLabel];
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"您正在登录账号  %@", self.UabOWQphoneNumberaYOEQN]];
    [attributeStr addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0, attributeStr.length)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:QTZ_Gray153 range:NSMakeRange(0, attributeStr.length)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:QTZ_Gray51 range:NSMakeRange(9, 11)];
    UILabel *edHyphtipsLabelTVYPVW = [UILabel new];
    edHyphtipsLabelTVYPVW.attributedText = attributeStr;
    [edHyphtipsLabelTVYPVW sizeToFit];
    edHyphtipsLabelTVYPVW.left = 40;
    edHyphtipsLabelTVYPVW.top = titleLabel.bottom + 16;
    [self.view addSubview:edHyphtipsLabelTVYPVW];
    
    self.dZrvUEtextFieldRRHEAt = [[UITextField alloc] initWithFrame:CGRectMake(38, edHyphtipsLabelTVYPVW.bottom + 60, FULL_WIDTH - 76, 25)];
    self.dZrvUEtextFieldRRHEAt.font = FONT(15);
    self.dZrvUEtextFieldRRHEAt.placeholder = @"请输入登陆密码";
    self.dZrvUEtextFieldRRHEAt.returnKeyType = UIReturnKeyDone;
    self.dZrvUEtextFieldRRHEAt.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.dZrvUEtextFieldRRHEAt.delegate = self;
    self.dZrvUEtextFieldRRHEAt.secureTextEntry = YES;
    [self.dZrvUEtextFieldRRHEAt.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0.0f, self.dZrvUEtextFieldRRHEAt.frame.size.height) toPoint:CGPointMake(self.dZrvUEtextFieldRRHEAt.frame.size.width, self.dZrvUEtextFieldRRHEAt.frame.size.height) color:QTZ_Gray132 borderWidth:.5]];
    [self.view addSubview:self.dZrvUEtextFieldRRHEAt];
    
    self.FDTTddnextBtnLAOJQZ = [[UIButton alloc] initWithFrame:CGRectMake(38, self.dZrvUEtextFieldRRHEAt.bottom + 35, self.dZrvUEtextFieldRRHEAt.width, 45)];
    self.FDTTddnextBtnLAOJQZ.titleLabel.font = FONT(12);
    [self.FDTTddnextBtnLAOJQZ setTitleColor:QTZ_White forState:0];
    [self.FDTTddnextBtnLAOJQZ setTitle:@"下一步" forState:0];
    [self.FDTTddnextBtnLAOJQZ setBackgroundImage:[UIImage imageNamed:@"icon_mine_btnBg"] forState:0];
    self.FDTTddnextBtnLAOJQZ.layer.cornerRadius = 5;
    [self.FDTTddnextBtnLAOJQZ addTarget:self action:@selector(handleLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.FDTTddnextBtnLAOJQZ];
    
    UIButton *forgetBtn = [UIButton new];
    forgetBtn.titleLabel.font = FONT(12);
    [forgetBtn setTitleColor:QTZ_Gray102 forState:0];
    [forgetBtn setTitle:@"找回密码" forState:0];
    [forgetBtn sizeToFit];
    forgetBtn.top = self.FDTTddnextBtnLAOJQZ.bottom + 5;
    forgetBtn.right = self.FDTTddnextBtnLAOJQZ.right;
    [forgetBtn addTarget:self action:@selector(handleForget) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
}

- (void)handleForget {
    PzwoeNJLCFForgetPassViewControllerVoiWYJ *vc = [PzwoeNJLCFForgetPassViewControllerVoiWYJ new];
    vc.UabOWQphoneNumberaYOEQN = self.UabOWQphoneNumberaYOEQN;
    [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:vc animated:YES];
}

- (void)handleLogin {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.UabOWQphoneNumberaYOEQN forKey:@"phone"];
    [params setSafeObject:[[NSString stringWithFormat:@"%@%@", [self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text MD5], self.UabOWQphoneNumberaYOEQN] MD5] forKey:@"password"];
    
    [sHkPtEJLCFMineRequestozAYZs loginWithParams:params loadingInView:self.view success:^{
        
        [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:MINE_REFRESH object:nil];
        [JLCFRegisterService shareService].phone = self.UabOWQphoneNumberaYOEQN;
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
}

- (BOOL)dZrvUEtextFieldRRHEAt:(UITextField *)dZrvUEtextFieldRRHEAt shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (dZrvUEtextFieldRRHEAt == self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text.length >= 15) {
            self.dZrvUEtextFieldRRHEAt.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:15];
            return NO;
        }
    }
    return YES;
}

- (BOOL)dZrvUEtextFieldRRHEAtShouldReturn:(UITextField *)dZrvUEtextFieldRRHEAt{
    return [dZrvUEtextFieldRRHEAt resignFirstResponder];
}

@end
